package com.jt.auth.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
@Slf4j
@Service
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private RemoteUserService remoteUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.jt.auth.pojo.User user = remoteUserService.doSelectByUsername(username);
        if (user == null){
            throw new UsernameNotFoundException("user id not exist");
        }
        List<String> permission = remoteUserService.doSelectPermission(user.getId());
        log.debug("permission{}",permission);

        User userDetails = new User(username, user.getPassword(),
                AuthorityUtils.createAuthorityList(permission.toArray(new String[]{})));
        return userDetails;
    }
}
